<?php

declare (strict_types=1);
/**
 * @copyright 深圳市易果网络科技有限公司
 * @version 1.0.0
 * @link https://dayiguo.com
 */

namespace App\Dao;

use App\Exception\CustomException;
use App\Kernel\Traits\DaoTrait;
use App\Kernel\Util\StoreJwtInstance;
use App\Model\Store;


use Hyperf\Database\Model\Builder;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;
use function JmesPath\search;

/**
 * class
 *
 * @author 张昊(2396675557@qq.com)
 * @package App\Dao
 */
class StoreDao extends Dao
{
    use DaoTrait;
    /**
     * @inheritDoc
     */
    public function model(): string
    {
        return Store::class;
    }

    /**
     * 通过ID获取用户
     *
     * @param int $id ID
     * @param bool $abnormal 是否抛异常
     * @return mixed
     */
    public function first(int $id, bool $abnormal = true)
    {
        $result = Store::query()->where('id', $id)->first();

        if (!$result && $abnormal === true) {
            $this->error('USER.NOT_FOUND');
        }

        return $result;
    }

    /**
     * 通过手机号码登陆或创建用户
     *
     * @param string $phone 手机号码
     * @return mixed
     */
    public function loginByPhone(string $phone)
    {
        return Store::query()->where(['phone' => $phone])->first();
    }

    /**
     * 获得所有用户通过map
     * @param array $map
     * @param string|null $sort
     * @param array|null $select
     * @param int|null $paginate
     * @return mixed
     */
    public function getStoresByMap(array $map = [], string $sort = null, array $select = null, int $paginate = null)
    {
        $model = self::makeModel();

        if (isset($map['between_field'])) {
            foreach ($map['between_field'] as $k => $value) {
                $model = $model->whereBetween($k, $value);
            }
            unset($map['between_field']);
        }

        // 或
        if (isset($map['or_field'])) {
            $model = $model->where(function (Builder $query) use($map) {
                foreach ($map['or_field'] as $field => $item) {
                    // 普通查询
                    if (!is_array($item)) {
                        $query->orWhere($field, $item);
                        continue;
                    }
                    // 其它方式
                    switch (strtolower($item[0])) {
                        case 'in':
                            $query->orWhereIn($field, $item[1]);
                            break;

                        default:
                            $query->orWhere($field, $item[0], $item[1]);
                    }
                }
            });

            unset($map['or_field']);
        }

        if (!empty($map)) {
            $model = $model->where(function ($query) use ($map) {
                return queryWhere($query, $map);
            });
        }
        if (null !== $sort) {
            $model = $model->orderByRaw($sort);
        }

        if (null !== $select) {
            $model = $model->select($select);
        }

        return $paginate > 0 ? $model->paginate($paginate) : $model->get();
    }



}